﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Boom.Navigation;

namespace Boom.GameObjects
{
    public class Boom:MultilFrame
    {
        private TimeSpan _ElapsedBoom;
        private TimeSpan _ElapsedDisp;

        private bool _Dispose;
        private bool _FinishTimeBoom;

        private int _TimeBoom;
        private ListFire _ListFires;

        public ListFire ListFires
        {
            get { return _ListFires; }
            set { _ListFires = value; }
        }

        public bool FinishTimeBoom
        {
            get { return _FinishTimeBoom; }
            set { _FinishTimeBoom = value; }
        }

        public bool DisposeBoom
        {
            get { return _Dispose; }
        }

        public TimeSpan ElapsedDisp
        {
            get { return _ElapsedDisp; }
            set { _ElapsedDisp = value; }
        }

        public TimeSpan ElapsedBoom
        {
            get { return _ElapsedBoom; }
            set { _ElapsedBoom = value; }
        }

        public Boom(Game Mygame, ref Texture2D TextureBoom,ref Texture2D TextureFire, Vector2 Position, int LenghtFire, List<int> PositionBrick, bool CheckObs)
            : base(Mygame, ref TextureBoom, 40, 40)
        {
            _Position = Position;
            _ListFires = new ListFire(Mygame, ref TextureFire, LenghtFire, PositionBrick, CheckObs);
            _ListFires.Initialize();
            _Bounds = new Rectangle((int)Position.X, (int)Position.Y, 40, 40);
            _ElapsedBoom = TimeSpan.Zero;
            _ElapsedDisp = TimeSpan.Zero;
            _Dispose = false;
            _FinishTimeBoom = false;
            _TimeBoom = Config.TimeBoom;
            _ActiveFrame = 0;
            Game.Components.Add(ListFires);
        }

        //Chuyển frame hình khi boom trong thời gian chờ nổ
        private void UpdateBoom(GameTime gameTime)
        {
            ListFires.Position = Position;
            _ElapsedBoom += gameTime.ElapsedGameTime;
            if (_ElapsedBoom > TimeSpan.FromMilliseconds(_TimeBoom * 1000 / 15) && !_FinishTimeBoom)
            {
                _ElapsedBoom -= TimeSpan.FromMilliseconds(_TimeBoom * 1000 / 15);
                ActiveFrame++;
                if (ActiveFrame > FrameList.Count - 1) ActiveFrame = FrameList.Count - 1;
            }
        }

        //Chuyển frame hình khi boom nổ
        private void UpdateDisposeBoom(GameTime gameTime)
        {
            if (!_Dispose)
            {
                _ElapsedDisp += gameTime.ElapsedGameTime;
                if (_ElapsedDisp > TimeSpan.FromSeconds(_TimeBoom) && !_FinishTimeBoom)
                {
                    _ElapsedDisp -= TimeSpan.FromSeconds(_TimeBoom);
                    _ActiveFrame = FrameList.Count - 1;
                    ListFires.CheckNewFire();
                    _FinishTimeBoom = true;
                }

                if (_ElapsedDisp > TimeSpan.FromMilliseconds(170) && _FinishTimeBoom)
                {
                    _ElapsedDisp -= TimeSpan.FromMilliseconds(170);
                    _Dispose = true;
                }
            }

            if (_Dispose )
            {
                Enabled = false;
                ListFires.Enabled = true;
            }
        }
 
        public override void Update(GameTime gameTime)
        {
            UpdateBoom(gameTime);
            UpdateDisposeBoom(gameTime);
            base.Update(gameTime);
        }        
    }


}
